package com.lm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lm.pojo.*;
import com.lm.service.ServeService;
import com.lm.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Lenovo
 * @version 1.0
 * @description: TODO
 * @date 2025/9/22 9:09
 */

@CrossOrigin
@Api(tags = "服务管理")
@RestController
@RequestMapping("/serve")
public class ServeController {
    @Autowired
    private ServeService serveService;

    @ApiOperation(value = "查询服务列表",httpMethod = "POST",notes = "查询所有待处理服务列表")
    @PostMapping(path = "/findList")
    public ResultUtil<IPage<Serve>> findList(@RequestBody Serve serve){
        //List<Serve> list = serveService.find(serve);
        try {
            IPage<Serve> pageInfo = serveService.find(serve);
            return new ResultUtil<>(200, "查询成功", pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil<>(500, "查询失败");
        }
    }

    @ApiOperation(value = "查询签约机构列表",httpMethod = "GET",notes = "查询所有签约机构名称列表")
    @GetMapping(path = "/findOrg")
    public ResultUtil<List<Organization>> findOrg(){
        List<Organization> list = serveService.findOrg();
        try {
            return new ResultUtil<>(200, "查询成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil<>(500, "查询失败");
        }
    }

    @ApiOperation(value = "查询医生团队列表",httpMethod = "GET",notes = "查询所有医生团队名称列表")
    @GetMapping(path = "/findDoctorTeam")
    public ResultUtil<List<DocterTeam>> findDoctorTeam(Integer orgId){
        List<DocterTeam> list = serveService.findDoctorTeam(orgId);
        try {
            return new ResultUtil<>(200, "查询成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil<>(500, "查询失败");
        }
    }


    @ApiOperation(value = "查询医生列表",httpMethod = "GET",notes = "查询团队中所有医生列表")
    @GetMapping(path = "/findDoctor")
    public ResultUtil<List<Docter>> findDoctor(Integer teamId){
        List<Docter> list = serveService.findDoctor(teamId);
        try {
            return new ResultUtil<>(200, "查询成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil<>(500, "查询失败");
        }
    }

    @ApiOperation(value = "查询服务包列表",httpMethod = "GET",notes = "查询所有服务包名称列表")
    @GetMapping(path = "/findServicePackage")
    public ResultUtil<List<ServicePackage>> findServicePackage(Integer teamId){
        List<ServicePackage> list = serveService.findServicePackage(teamId);
        try {
            return new ResultUtil<>(200, "查询成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil<>(500, "查询失败");
        }
    }

    @ApiOperation(value = "查询服务项目列表",httpMethod = "GET",notes = "查询所有服务项目名称列表")
    @GetMapping(path = "/findItem")
    public ResultUtil<List<ServiceItem>> findItem(Integer packageId){
        List<ServiceItem> list = serveService.findItem(packageId);
        try {
            return new ResultUtil<>(200, "查询成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil<>(500, "查询失败");
        }
    }

    @ApiOperation(value = "查询居民信息列表",httpMethod = "GET",notes = "查询居民所有信息列表")
    @GetMapping(path = "/search")
    public ResultUtil<List<Resident>> search(String name){
        List<Resident> list = serveService.search(name);
        try {
            return new ResultUtil<>(200, "查询成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil<>(500, "查询失败");
        }
    }

    @ApiOperation(value = "查询居民家庭列表",httpMethod = "GET",notes = "查询居民所有家庭成员列表")
    @GetMapping(path = "/familyMembers")
    public ResultUtil<List<Resident>> familyMembers(Integer residentId){
        List<Resident> list = serveService.familyMembers(residentId);
        try {
            return new ResultUtil<>(200, "查询成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil<>(500, "查询失败");
        }
    }

    @ApiOperation(value = "添加服务列表",httpMethod = "POST",notes = "添加服务列表所有信息")
    @PostMapping(path = "/saveArchive")
    public ResultUtil insert(@RequestBody Serve serve){
        serveService.insert(serve);
        try{
            return new ResultUtil(200, "添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil(500, "添加失败");
        }
    }

    @ApiOperation(value = "查询居民详情列表",httpMethod = "GET",notes = "查询居民所有服务列表")
    @GetMapping(path = "/findServiceById")
    public ResultUtil<Serve> findServiceById(Integer residentId){
        Serve serve = serveService.findServiceById(residentId);
        try {
            return new ResultUtil<>(200, "查询成功",serve);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil<>(500, "查询失败");
        }
    }

    @ApiOperation(value = "修改为已完成服务状态列表",httpMethod = "GET",notes = "修改服务状态列表")
    @GetMapping (path = "/updateState")
    public ResultUtil updateState(Integer serviceRecordId){
        serveService.updateState(serviceRecordId);
        try{
            return new ResultUtil(200, "修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil(500, "添加失败");
        }
    }
    @ApiOperation(value = "添加健康列表",httpMethod = "POST",notes = "添加健康列表所有信息")
    @PostMapping(path = "/insertHealth")
    public ResultUtil insertHealth(@RequestBody ServiceHealthInfo serviceHealthInfo){
        serveService.insertHealth(serviceHealthInfo);
        try{
            return new ResultUtil(200, "添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil(500, "添加失败");
        }
    }


    @ApiOperation(value = "查询已结束服务列表",httpMethod = "POST",notes = "查询所有已结束服务列表")
    @PostMapping(path = "/findLists")
    public ResultUtil<IPage<Serve>> findLists(@RequestBody Serve serve){
        try {
            IPage<Serve> pageInfo = serveService.findLists(serve);
            return new ResultUtil<>(200, "查询成功", pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil<>(500, "查询失败");
        }
    }

    @ApiOperation(value = "修改为已取消服务状态列表",httpMethod = "GET",notes = "修改服务状态列表")
    @GetMapping (path = "/updateStates")
    public ResultUtil updateStates(Integer serviceRecordId,String serviceCancel){
        serveService.updateStates(serviceRecordId,serviceCancel);
        try{
            return new ResultUtil(200, "修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil(500, "添加失败");
        }
    }

    @ApiOperation(value = "修改为已驳回服务状态列表",httpMethod = "GET",notes = "修改服务状态列表")
    @GetMapping (path = "/updateStateBs")
    public ResultUtil updateStateBs(Integer serviceRecordId,String serviceInfo){
        serveService.updateStateBs(serviceRecordId,serviceInfo);
        try{
            return new ResultUtil(200, "修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil(500, "添加失败");
        }
    }

    @ApiOperation(value = "修改服务信息列表",httpMethod = "POST",notes = "修改服务服务信息部分列表")
    @PostMapping(path = "/saveService")
    public ResultUtil saveService(@RequestBody Serve serve){
        try {
            serveService.saveServices(serve);
            return new ResultUtil<>(200, "查询成功", serve);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil<>(500, "查询失败");
        }
    }

    @ApiOperation(value = "重新上传服务项目列表",httpMethod = "GET",notes = "重新上传服务项目列表")
    @GetMapping (path = "/updateStateBes")
    public ResultUtil updateStateBes(Integer serviceRecordId){
        serveService.updateStateBes(serviceRecordId);
        try{
            return new ResultUtil(200, "修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil(500, "添加失败");
        }
    }
}
